package com.theme.passport.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.theme.common.mvc.utils.PageUtils;
import com.theme.common.mvc.validator.ValidatorUtils;
import com.theme.common.mvc.view.R;
import com.theme.passport.annotation.Login;
import com.theme.passport.controller.app.utils.AppContextUtil;
import com.theme.passport.entity.UserEntity;
import com.theme.passport.entity.constant.DownloadTypeEnum;
import com.theme.passport.entity.theme.BrowseHistoryEntity;
import com.theme.passport.entity.theme.ThemeEntity;
import com.theme.passport.entity.theme.WallpaperEntity;
import com.theme.passport.entity.theme.WidgeEntity;
import com.theme.passport.form.DownloadBrowseLikeBuyForm;
import com.theme.passport.service.theme.BrowseHistoryService;
import com.theme.passport.service.theme.ThemeService;
import com.theme.passport.service.theme.WallpaperService;
import com.theme.passport.service.theme.WidgeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("app/browse")
@Api("用户浏览记录")
public class BrowseHistoryController {
    @Autowired
    private BrowseHistoryService browseHistoryService;

    @Autowired
    private WallpaperService wallpaperService;

    @Autowired
    private ThemeService themeService;

    @Autowired
    private WidgeService widgeService;

    @Login
    @GetMapping("/list")
    @ApiOperation("查询用户浏览记录接口")
    public R list(@RequestParam Map<String, Object> params){
        UserEntity currentUser = AppContextUtil.getCurrentUser();
        params.put("userId",currentUser.getId() +"");
        PageUtils page = browseHistoryService.queryPage(params);
        return R.ok().put("page", page);
    }

    @Login
    @PostMapping("/add")
    @ApiOperation("添加用户浏览记录的接口")
    public R add(@RequestBody DownloadBrowseLikeBuyForm form){
        //表单校验
        ValidatorUtils.validateEntity(form);

        BrowseHistoryEntity browseHistory = new BrowseHistoryEntity();
        UserEntity currentUser = AppContextUtil.getCurrentUser();
        browseHistory.setType(form.getType());
        browseHistory.setObjectId(form.getObjectId());
        browseHistory.setUserId(currentUser.getId());
        browseHistory.setCreated(new Date());

        QueryWrapper<BrowseHistoryEntity> orderWrapper = new QueryWrapper<>();
        orderWrapper.and(wrapper -> wrapper
                .eq("user_id", currentUser.getId())
                .eq("type", browseHistory.getType())
                .eq("object_id", browseHistory.getObjectId()));
        List<BrowseHistoryEntity> list = browseHistoryService.list(orderWrapper);
        if (list != null && list.size()>0){
            return R.ok();
        }
        if (browseHistory.getType() == DownloadTypeEnum.THEME.getValue()){
            ThemeEntity theme = themeService.getById(browseHistory.getObjectId());
            if (theme != null){
                browseHistory.setObjectName(theme.getName());
                browseHistory.setImage(theme.getIcon());
            }
        }else if (browseHistory.getType() == DownloadTypeEnum.WIDGE.getValue()){
            WidgeEntity widge = widgeService.getById(browseHistory.getObjectId());
            if (widge != null){
                browseHistory.setObjectName(widge.getName());
                browseHistory.setImage(widge.getPreview());
            }
        }else {
            WallpaperEntity wallpaper = wallpaperService.getById(browseHistory.getObjectId());
            if (wallpaper != null){
                browseHistory.setObjectName(wallpaper.getName());
                browseHistory.setImage(wallpaper.getImage());
            }
        }
        browseHistoryService.save(browseHistory);
        return R.ok();
    }

    @Login
    @PostMapping("/delete")
    @ApiOperation("删除用户浏览记录接口 id数组")
    public R delete(@RequestBody Long[] ids){
		browseHistoryService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }
}
